﻿using Sunny.UI;
using System.Collections.Generic;
using CoreShop.Model;
using CoreShop.Services;
using System;
using CoreShop.Model.UI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections;
using System.Security.Policy;
using SqlSugar.Extensions;
using Sunny.UI.Win32;
using System.Windows.Forms;
using System.ComponentModel;
using CoreShop.Library;
using System.Data;
using FastMember;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Linq;

namespace CoreShop.App
{
    public partial class frmScaleList : UIForm
    {
        public bool returnValue;
        public List<corecmsscale> scaleList = new List<corecmsscale>();
        public string number = "";
        private bool isSelect = false;
        public corecmsscale selectModel = null;

        public frmScaleList(string _number,bool _isSelect)
        {
            number = _number;
            isSelect =_isSelect;
            InitializeComponent();
        }
        public frmScaleList()
        {
            InitializeComponent();            
        }

        private string GetMenuItemName(long orderingId)
        {
            return "uiMenuItem_" + orderingId.ToString();
        }

        #region 加载信息列表
        private void LoadDataList()
        {            
            try
            {
                layOrdering.Visible = false;
                layOrdering.Clear();
                foreach (var item in scaleList)
                {
                    UIScaleDetailItem item1 = new UIScaleDetailItem(item);
                    item1.Name = GetMenuItemName(item.id);
                    item1.Tag = item.id.ToString();
                    item1.DeleteInfo += DoDeleteInfo;
                    layOrdering.Add(item1);
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message, "系统出错-BindOrdering:", UIStyle.Green, UIMessageBoxButtons.OK);
            }
            finally
            {
                layOrdering.Visible = true;
            }
        }

        private void DoDeleteInfo(object sender, System.EventArgs e)
        {
            UIScaleDetailItem item = (UIScaleDetailItem)sender;
            var packId = CommFunc.ConvertToBigInt(item.Tag);
            var model = scaleList.Find(p => p.id == packId);
            if (model != null)
            {
                var bl = new corecmsscaleServices().DeleteById(model.id);
                scaleList.Remove(model);
                layOrdering.Remove(item);
                layOrdering.Refresh();
            }
        }

        #endregion

        private void FMember_Load(object sender, System.EventArgs e)
        {
            if (!isSelect) btnSelect.Visible = false;
            scaleList = new corecmsscaleServices().GetList();
            //this.SetStyle(AppConstVars.sysStyle);
            LoadDataList();
        }

        private void btnAddInfo_Click(object sender, System.EventArgs e)
        {
            ScaleBind();
            corecmsscale scale = new corecmsscale();
            scale.id = (long)Yitter.IdGenerator.YitIdHelper.NextId();
            scale.scaleType = (int)GlobalEnumVars.ScaleBarcodeType.Nothing;
            scaleList.Add(scale);
            LoadDataList();
        }

        private void btnSerach_Click(object sender, EventArgs e)
        {
            LoadDataList();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            ScaleBind();
            this.DialogResult = DialogResult.Yes;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ScaleBind()
        {
            var newList = new List<corecmsscale>();
            foreach (var item in scaleList)
            {
                var mControl = layOrdering.Get(GetMenuItemName(item.id));
                if (mControl == null) continue;
                var modelItem = (UIScaleDetailItem)mControl;
                newList.Add(modelItem.GetModelValue());
            }
            scaleList = newList;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ScaleBind();
            var bl = new corecmsscaleServices().InsertOrUpdate(scaleList);
            if (bl)
            {
                ShowInfoTip("保存成功。");
            }
        }
    }
}